import { reqNew, reqGroupon, reqSeck, reqGoods, reqCate } from "../../request/api"

//初始状态
const initState = {
    news: [],//新人专享
    goods: [],//首页商品
    groupon: [],//万人团
    seckill: [],//秒杀
    cates: [],//商品分类
    n: 0,
    newsTime: {h: 0, m: 0, s: 0 },//新人专享倒计时
    grouponTime: { h: 0, m: 0, s: 0 },//万人团倒计时
    seckillTime: { h: 0, m: 0, s: 0 }//秒杀倒计时

}

// actionType
const TYPES = {
    HOME_CHANGE_NEWS: 'HOME_CHANGE_NEWS',
    HOME_CHANGE_GOODS: 'HOME_CHANGE_GOODS',
    HOME_CHANGE_GROUPON: 'HOME_CHANGE_GROUPON',
    HOME_CHANGE_CATES: 'HOME_CHANGE_CATES',
    HOME_CHANGE_SECKILL: 'HOME_CHANGE_SECKILL',
    HOME_CHANGE_N: 'HOME_CHANGE_N',
    HOME_CHANGE_NEWSTIME:'HOME_CHANGE_NEWSTIME'
}

// reducer
const reducer = (state = initState, action) => {
    switch (action.type) {
        // 修改新人专享
        case TYPES.HOME_CHANGE_NEWS:
            return {
                ...state,
                news: action.news
            }
        // 修改首页商品
        case TYPES.HOME_CHANGE_GOODS:
            return {
                ...state,
                goods: action.goods
            }
        // 修改万人团
        case TYPES.HOME_CHANGE_GROUPON:
            return {
                ...state,
                groupon: action.groupon
            }
        // 修改秒杀
        case TYPES.HOME_CHANGE_SECKILL:
            return {
                ...state,
                seckill: action.seckill
            }
        // 修改分类
        case TYPES.HOME_CHANGE_CATES:
            return {
                ...state,
                cates: action.cates
            }
        // 修改n
        case TYPES.HOME_CHANGE_N:
            return {
                ...state,
                n: action.n
            }
             // 修改newsTime
        case TYPES.HOME_CHANGE_NEWSTIME:
            return {
                ...state,
                newsTime: action.newsTime
            }
        default:
            return state;
    }
}

// actionCreators
export const actions = {
    // 修改新人专享
    changeNews: (news) => ({ type: TYPES.HOME_CHANGE_NEWS, news }),
    // 请求新人专享数据
    requestNew: () => {
        return (dispatch, getState) => {
            // 判断状态层是否有数据，没有数据才请求
            let news = getNews(getState())
            if (news.length > 0) {
                return;
            }
            reqNew().then(res => {
                if (res.data.code == 200) {
                    dispatch(actions.changeNews(res.data.list))
                }
            })
        }
    },
    // 修改首页商品
    changeGoods: (goods) => ({ type: TYPES.HOME_CHANGE_GOODS, goods }),
    // 请求首页商品
    requestGoods: () => {
        return (dispatch, getState) => {
            // 判断状态层是否有数据，没有数据才请求
            let goods = getGoods(getState())
            if (goods.length > 0) {
                return;
            }
            reqGoods().then(res => {
                if (res.data.code == 200) {
                    dispatch(actions.changeGoods(res.data.list))
                }
            })
        }
    },
    // 修改万人团
    changeGroupon: (groupon) => ({ type: TYPES.HOME_CHANGE_GROUPON, groupon }),
    // 请求万人团
    requestGroupon: () => {
        return (dispatch, getState) => {
            // 判断状态层是否有数据，没有数据才请求
            let groupon = getGroupon(getState())
            if (groupon.length > 0) {
                return;
            }
            reqGroupon().then(res => {
                if (res.data.code == 200) {
                    dispatch(actions.changeGroupon(res.data.list))
                }
            })
        }
    },
    // 修改秒杀
    changeSeckill: (seckill) => ({ type: TYPES.HOME_CHANGE_SECKILL, seckill }),
    // 请求秒杀
    requestSeckill: () => {
        return (dispatch, getState) => {
            // 判断状态层是否有数据，没有数据才请求
            let seckill = getGoods(getState())
            if (seckill.length > 0) {
                return;
            }
            reqSeck().then(res => {
                if (res.data.code == 200) {
                    dispatch(actions.changeSeckill(res.data.list))
                }
            })
        }
    },
    // 修改分类
    changeCates: (cates) => ({ type: TYPES.HOME_CHANGE_CATES, cates }),
    // 请求分类
    requestCates: () => {
        return (dispatch, getState) => {
            // 判断状态层是否有数据，没有数据才请求
            let cates = getGoods(getState())
            if (cates.length > 0) {
                return;
            }
            reqCate().then(res => {
                if (res.data.code == 200) {
                    dispatch(actions.changeCates(res.data.list))
                }
            })
        }
    },
    // 修改n
    changeN: (n) => ({ type: TYPES.HOME_CHANGE_N, n }),
    // 修改newsTime
    changeNewsTime:newsTime=>(dispatch,getState)=>{
        if(newsTime.leftTime>0){
            newsTime.flag=true;
        }
        dispatch({type:TYPES.HOME_CHANGE_NEWSTIME,newsTime})
    }
}
// selectors
export const getNews = state => state.home.news;
export const getGoods = state => state.home.goods;
export const getGroupon = state => state.home.groupon;
export const getSeckill = state => state.home.seckill;
export const getCates = state => state.home.cates;
export const getN = state => state.home.n;
export const getNewsTime=state=>state.home.newsTime;
// 展示到页面的数据
export const showGoods = state => {
    let goods = getGoods(state);
    let n = getN(state);
    return goods.length === 0 ? [] : goods[n].content
}

export default reducer